import 'dart:io';

import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sainbainu/common/routes/routes.dart';
import 'package:sainbainu/common/services/app.dart';
import 'package:sainbainu/common/services/jpush.dart';
import 'package:sainbainu/common/values/colors.dart';

class AgreePolicyDialog extends StatefulWidget {
  final Widget child;

  const AgreePolicyDialog({super.key, required this.child});

  @override
  State<AgreePolicyDialog> createState() => _AgreePolicyDialogState();
}

class _AgreePolicyDialogState extends State<AgreePolicyDialog> {
  late TapGestureRecognizer _agreeGestureRecognizer;
  late TapGestureRecognizer _policyGestureRecognizer;

  @override
  void initState() {
    super.initState();
    // 等首帧渲染完毕
    WidgetsBinding.instance.addPostFrameCallback((_) {
      if (AppService.to.isFirstOpenApp) {
        _showPrivacyPolicyDialog();
      }
    });
    // 给 textspan 绑定点击事件
    _agreeGestureRecognizer = TapGestureRecognizer();
    _agreeGestureRecognizer.onTap = () {
      Get.toNamed(AppRoutes.userAgree);
    };
    _policyGestureRecognizer = TapGestureRecognizer();
    _policyGestureRecognizer.onTap = () {
      Get.toNamed(AppRoutes.privacyAgree);
    };
  }

  /// 同意协议
  onConfirm() {
    AppService.to.setFirstOpenApp(false);
    JpushService.to.applyPushAuthority();
    Get.back();
  }

  /// 退出应用
  onExitApp() {
    exit(0);
  }

  /// 显示隐私弹窗
  _showPrivacyPolicyDialog() {
    // showCupertinoDialog(
    //   context: context,
    //   builder: (_) => CupertinoAlertDialog(
    //     title: Text('个人信息保护指引'),
    //     content: Container(
    //       height: 240,
    //       padding: EdgeInsets.all(12.0),
    //       child: Text('hahahahahahahahahahahahahahaha的哈金山打卡dkhvk'),
    //     ),
    //     actions: [
    //       CupertinoDialogAction(child: Text('拒绝并退出'), onPressed: () {}),
    //       CupertinoDialogAction(child: Text('同意'), onPressed: () {}),
    //     ],
    //   ),
    // );
    showGeneralDialog(
      context: context,
      barrierColor: const Color.fromRGBO(0, 0, 0, 0.5),
      pageBuilder: (_, a, b) => _buildDialog(),
    );
  }

  Widget _buildDialog() {
    return Center(
      child: Container(
        width: 280,
        padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 15),
        decoration: const BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.all(Radius.circular(15)),
        ),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            _buildTitle(),
            const SizedBox(height: 10),
            _buildContent(),
            const SizedBox(height: 30),
            _buildBtns(),
          ],
        ),
      ),
    );
  }

  Widget _buildTitle() {
    return const Text(
      '个人信息保护指引',
      style: TextStyle(
        fontSize: 17,
        fontWeight: FontWeight.w600,
        color: AppColors.active,
      ),
    );
  }

  Widget _buildContent() {
    return RichText(
      text: TextSpan(
        text: '感谢您使用SainBainu，您在使用该产品前，请认真阅读并充分理解',
        style: const TextStyle(
          color: AppColors.active2,
          fontSize: 14,
        ),
        children: [
          TextSpan(
            text: '《用户协议》',
            style: const TextStyle(
              color: AppColors.primary,
            ),
            recognizer: _agreeGestureRecognizer,
          ),
          const TextSpan(text: '和'),
          TextSpan(
            text: '《隐私政策》',
            style: const TextStyle(
              color: AppColors.primary,
            ),
            recognizer: _policyGestureRecognizer,
          ),
          const TextSpan(text: '如您同意，请点击"同意并继续"并接受我们的服务，感谢您的信任。'),
        ],
      ),
    );
  }

  Widget _buildBtns() {
    return Row(
      children: [
        TextButton(
          onPressed: () => onExitApp(),
          style: TextButton.styleFrom(
            foregroundColor: AppColors.unactive,
          ),
          child: const Text('拒绝并退出'),
        ),
        const SizedBox(width: 10),
        Expanded(
          child: ElevatedButton(
            onPressed: () => onConfirm(),
            style: ElevatedButton.styleFrom(
              backgroundColor: AppColors.danger,
              foregroundColor: Colors.white,
              enableFeedback: true,
            ),
            child: const Text('同意并继续'),
          ),
        ),
      ],
    );
  }

  @override
  Widget build(BuildContext context) => widget.child;
}
